package com.example.java_gobang.api;

import com.example.java_gobang.contant.Contants;
import com.example.java_gobang.model.Result;
import com.example.java_gobang.model.UserInfo;
import com.example.java_gobang.service.UserInfoService;
import com.example.java_gobang.utils.JwtUtils;
import com.example.java_gobang.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserInfoApi {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/login")
    @ResponseBody
    public Result login(String username, String password, HttpSession session){
        log.info("登录的用户名:{} 密码{}",username,password);
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
            return Result.fail("密码和用户名不能为空");
        }
        //验证名字
        UserInfo userInfo = userInfoService.selectByName(username);
        if(userInfo == null){
            return Result.fail("查无此人");
        }
        //验证密码
        Boolean ret = SecurityUtils.verify(password,userInfo.getPassword());
        if(ret == false){
            return Result.fail("密码错误");
        }
        //设置session
        session.setAttribute("user",userInfo);
        //返回token
        Map<String,Object> map = new HashMap<>();
        map.put(Contants.TOKEN_USER_USERANME,userInfo.getUsername());
        map.put(Contants.TOKEN_USER_ID,userInfo.getUserId());
        System.out.println("userid："+userInfo.getUserId());
        return Result.success(JwtUtils.getToken(map));
    }
    //注册
    @RequestMapping("/register")
    @ResponseBody
    public Result register(UserInfo userInfo){

        log.info("注册的用户名:{} 密码{}",userInfo.getUsername(),userInfo.getPassword());
        if(!StringUtils.hasLength(userInfo.getUsername()) || !StringUtils.hasLength(userInfo.getPassword())){
            return Result.fail("密码和用户名不能为空");
        }
        //加密
        String newPassword = SecurityUtils.encrypt(userInfo.getPassword());
        userInfo.setPassword(newPassword);
        //插入
        Integer ret;
        try {
            ret = userInfoService.insertUser(userInfo.getUsername(),userInfo.getPassword());
        }catch (Exception e){
            return Result.fail("注册失败");
        }

        if(ret != 1){
            return Result.fail("注册失败");
        }
        return Result.success("");
    }
    //获取用户信息
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo(HttpServletRequest request){
        String token = request.getHeader(Contants.USER_TOKEN_HANDER);
        //根据token拿到用户id
        Integer id = JwtUtils.getIdByToken(token);
        log.info("用户ID: ",id);
        //查找用户
        UserInfo userInfo = userInfoService.selectById(id);
        userInfo.setPassword("");
        if(userInfo == null){
            return Result.fail("未查找到该用户");
        }
        return Result.success(userInfo);
    }
}
